﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text.RegularExpressions;
using LifeInSharePoint.SPTwitter.WebParts;
using System.Web.UI.HtmlControls;

namespace LifeInSharePoint.SPTwitter.WebParts.MyMessages
{
    [ToolboxItemAttribute(false)]
    public class MyMessages : WebPart
    {
        private static string TwitterList = "SPTwitter";


        private const uint defaultNumberOfTweets = 10;
        private uint numberOfTweets = defaultNumberOfTweets;
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [Category("Parameters")]
        [WebDisplayName("Number Of Tweets to show")]
        [Description("")]
        public uint NumberOfTweets
        {
            get { return numberOfTweets; }
            set { numberOfTweets = value; }
        }


       protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);

            try
            {
                using (SPWeb web = SPContext.Current.Web)
                {
                    if (web != null)
                    {
                        web.AllowUnsafeUpdates = true;

                        SPList SPTwitterList = web.Lists[TwitterList];
                        SPListItemCollection items = GetAllTweets(SPTwitterList, numberOfTweets);
                        if (items.Count > 0)
                        {
                            writer.Write("<table cellpadding='3' cellspacing='0' border='0'>");
                            foreach (SPListItem item in items)
                            {
                                writer.Write("<tr>");
                                    writer.Write("<td>");
                                        writer.Write("<div style='margin:5px 5px 0px 0px;'>");
                                            writer.Write("<img src='" + Common.GetUserProfilePicture(item) + "' border='0'/>");
                                        writer.Write("</div>");
                                    writer.Write("</td>");
                                    writer.Write("<td style='margin-left:5px;'>");
                                        writer.Write("<div>");
                                            writer.Write(Common.AddWebAndTwitterLinks(item["Title"].ToString()) + "<br />");
                                            writer.Write("<div style='color:#bdbabd;'>" + Common.GetDifferenceDate((DateTime)item["Modified"], DateTime.Now) + " by " + Common.GetUserDetails(item) + "</div><br />");
                                        writer.Write("</div>");
                                    writer.Write("</td>");
                                writer.Write("</tr>");
                            }
                            writer.Write("</table>");
                        }
                        else
                        {
                            writer.Write("No Messages");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                writer.Write("Error Displaying Messages : " + ex.Message);
            }
        }

        public static SPListItemCollection GetAllTweets(SPList SPTwitterList, uint numberOfTweets)
        {
            SPListItemCollection items = null;
            try
            {
                SPQuery query = new SPQuery();
                SPUser CurrentUser = SPContext.Current.Web.CurrentUser;
                query.Query = string.Format("<Where><Contains><FieldRef Name='TwitterMentions' /><Value Type='Text'>{0}</Value></Contains></Where><OrderBy><FieldRef Name='Created' Ascending='FALSE'/></OrderBy>", CurrentUser.Name.Replace(" ",""));
                items = SPTwitterList.GetItems(query);
                query.RowLimit = numberOfTweets;
            }
            catch (Exception ex)
            {
               //tils.LogError(ex);
            }

            return items;
        }

    }
}
